# -*- coding: utf-8 -*-
import scrapy

from BI_Spider.items import TianmaoItem


class TianmaoSpider(scrapy.Spider):
    name = 'tianmao'
    allowed_domains = ['tianmao.com']
    start_urls = [
        # 完美日记
        # 'https://list.tmall.com/search_product.htm?q=%E5%AE%8C%E7%BE%8E%E6%97%A5%E8%AE%B0&type=p&vmarket=&spm=875.7931836%2FB.a2227oh.d100&from=mallfp..pc_1_searchbutton',

        # WIS旗舰店
        # 'https://list.tmall.com/search_shopitem.htm?user_id=1665051977&cat=2&spm=a220m.1000858.a2227oh.d100&oq=WIS%C6%EC%BD%A2%B5%EA&from=.list.pc_1_searchbutton&ds=1&stype=search',

        # 透蜜旗舰店
         'https://list.tmall.com/search_shopitem.htm?user_id=850069620&cat=2&spm=a220m.1000862.a2227oh.d100&oq=%CD%B8%C3%DB%C6%EC%BD%A2%B5%EA&from=.list.pc_1_searchbutton&ds=1&stype=search',
    ]

    custom_settings = {
        'ITEM_PIPELINES': {'BI_Spider.pipelines.TianmaoPipeline': 300},
    }

    def parse(self, response):
        node_list = response.xpath("//div[@class='product-iWrap']")

        for node in node_list:
            item = TianmaoItem()
            item['jumpLink'] = node.xpath("./div[@class='productImg-wrap']/a/@href").extract()[0]

            temp_image_link = node.xpath("./div[@class='productImg-wrap']/a/img/@src").extract()

            if len(temp_image_link) != 0:
                item['imageLink'] = temp_image_link[0]
            else:
                item['imageLink'] = ""

            price = node.xpath("./p[@class='productPrice']/em//text()").extract()
            if len(price) != 0:
                item['price'] = price[0] + price[1]
            else:
                item['price'] = ''

            temp = node.xpath("./div[@class='productTitle productTitle-spu']")

            # 商品名称
            goods_name = ''
            if len(temp) != 0:
                temp_goods_name = node.xpath("./div[@class='productTitle productTitle-spu']/a[1]//text()").extract()
                describe = node.xpath("./div[@class='productTitle productTitle-spu']/a[2]/text()").extract()

                if len(describe) != 0:
                    describe_name = describe[0]
                else:
                    describe_name = ""

                if len(temp_goods_name) != 0:
                    goods_name = ''.join(temp_goods_name) + "  " + describe_name
            else:
                temp_goods_name = node.xpath("./p[@class='productTitle']/a/text()").extract()
                if len(temp_goods_name) != 0:
                    goods_name = temp_goods_name[0]
            item['goods_name'] = goods_name

            item['shop_name'] = node.xpath("./div[@class='productShop']/a/text()").extract()[0].replace("\n", "")

            # 月销量
            month_number = node.xpath("./p[@class='productStatus']/span/em/text()").extract()
            if len(month_number) != 0:
                item['month_number'] = month_number[0]
            else:
                item['month_number'] = ''

            # 评论数
            comment_number = node.xpath("./p[@class='productStatus']/span[2]/a/text()").extract()
            if len(comment_number) != 0:
                item['comment_number'] = comment_number[0]
            else:
                item['comment_number'] = ""

            yield item
